import { _decorator, Node } from 'cc';
import { StateDefine } from '../../Config';
import { PlayerState } from './PlayerState';
const { ccclass, property } = _decorator;

@ccclass('PlayerAttackState')
export class PlayerAttackState extends PlayerState {

    private comboCounter: number = 0;
    private lastTimeAttacked: number = null;
    private comboWindow: number = 2000;
    
    onEnter() {
        this.stateTime = 0.1;
        
        if (this.comboCounter > 2 || this.lastTimeAttacked + this.comboWindow < new Date().getTime()) this.comboCounter = 0;
        this.anim = StateDefine.Attack + (this.comboCounter + 1);
        super.onEnter();

        let attackDir = this.player.facingDir;
        if(this.player.input.x!=0)attackDir = this.player.input.x;
        this.player.setVelocity(this.player.AttackMovement[this.comboCounter]*attackDir,0);
    }

    onExit() {
        super.onExit();
        this.lastTimeAttacked = new Date().getTime();
        this.comboCounter++;
    }

    update(deltaTime: number) {

        super.update(deltaTime);
        if ((this.stateTime) < 0){
            this.player.ZeroVelocity();
        }
        // this.player.setVelocity(this.player.input.x*0.01,0);
        if (this.triggerCalled)
            this.player.stateMgr.transit(StateDefine.Idle);
    }

    onDestory() { }

    canTransit(to: StateDefine): boolean {
        // if (this.triggerCalled || StateDefine.Dash === to) {
        return true;
        // }
        return false;
    }
}


